package com.walkline.screen;

import java.util.Hashtable;
import java.util.Vector;

import net.rim.blackberry.api.browser.Browser;
import net.rim.blackberry.api.browser.BrowserSession;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

import com.walkline.util.Function;
import com.walkline.util.container.VerticalButtonFieldSet;
import com.walkline.weibo.WeiboSDK;

public class ShowUsersLinksPopupScreen extends PopupScreen
{
	private WeiboSDK _weibo;
	private ButtonField[] _buttonList;

	public ShowUsersLinksPopupScreen(WeiboSDK weibo, Vector lists, final boolean isLinks)
	{
		super(new VerticalFieldManager(VERTICAL_SCROLL | VERTICAL_SCROLL_MASK));

		_weibo = weibo;

		VerticalButtonFieldSet vbf = new VerticalButtonFieldSet(USE_ALL_WIDTH);

		_buttonList = new ButtonField[lists.size()];
		for (int i=0; i< lists.size(); i++)
		{
			_buttonList[i] = new ButtonField((String) lists.elementAt(i), ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY);
			_buttonList[i].setChangeListener(new FieldChangeListener()
			{
				public void fieldChanged(Field field, int context)
				{
					if (isLinks)
					{
						BrowserSession browser = Browser.getDefaultSession();
			    		browser.displayPage(((ButtonField) field).getLabel());	
					} else {
						Hashtable params = new Hashtable();
						String screen_name = ((ButtonField) field).getLabel();

						screen_name = screen_name.substring(1);
						params.put("uid", "0");
						params.put("screen_name", screen_name);

						UiApplication.getUiApplication().pushScreen(new HomePageMainScreen(_weibo, params));
					}

		    		onClose();
				}
			});

			vbf.add(_buttonList[i]);
		}

		//add(headLine);
		//add(new SeparatorField());
		add(vbf);
	}
	
	public boolean onClose()
	{
		UiApplication.getUiApplication().popScreen(this);					

		return true;
	}
}